package com.alperez.recolourbitmap.data;

import android.text.TextUtils;

public class ColourKeeper {
	public static final String COLOR_STRING_PREFIX = "#";
	
	private int mColour;
	private String mStringValue;
	
	/**
	 * colour must be provided in #RRGGBB format
	 * @param strColour
	 */
	public ColourKeeper(String strColour) {
		if (!ColourKeeper.isValidStringRepresentation(strColour)) {
			throw new IllegalArgumentException("Valid string representation of colour must be provided (#RRGGBB). Currently provided - "+strColour);
		}
		mStringValue = strColour;
		mColour = Integer.parseInt(strColour.substring(1), 16) - 16777216; //x - 1000000h
	}
	
	public int getColour() {
		return mColour;
	}

	public String getColourString() {
		return mStringValue;
	}
	
	/**
	 * This method checks whether provided String is a valid colour.
	 * @param strColour
	 * @return
	 */
	public static boolean isValidStringRepresentation(String strColour) {
		if (TextUtils.isEmpty(strColour)) return false;
		
		if ((strColour.length() != 7) || !strColour.startsWith(COLOR_STRING_PREFIX)) {
			return false;
		}
		try {
			Integer.parseInt(strColour.substring(1), 16);
		} catch(Exception e){
			return false;
		}
		return true;
	}
}
